<?php

// @formatter:off
/**
 * @file WCFVBoxes.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: WCFVBoxes.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/includes/WCFVBoxes.php $
 */
// @formatter:on

class WCFVBoxes extends WCFactory {
	public static function GetBundlePreviewAssigns(WCRepresentation &$item, $css = false, $info = array()) {
		$out = array();

		//
		// Basic information.
		$out["box_id"] = $item->id();
		$out["box_name"] = $item->name();
		$out["box_info"] = $info;
		$out["box_preview"] = $item->preview();
		$out["box_previews"] = $item->previews();

		if($css) {
			$out["box_css"] = $css;
		}

		return $out;
	}
	public static function GetCollectionPreviewAssigns(WCCollection &$collection, $css = false) {
		$info = array();
		self::ExpandInfo($info, $collection);

		$out = self::GetBundlePreviewAssigns($collection, $css, $info);

		$out["box_what"] = "collection";
		$out["box_action"] = WC_ACTION_COLLECTION_SHOW;

		return $out;
	}
	public static function GetComicPreviewAssigns(WCComic &$comic, $css = false) {
		$out = array();

		global $wcProfile;

		//
		// Basic information
		$out["box_id"] = $comic->id();
		$out["box_image_id"] = "IMG_CX_{$comic->id()}";
		$out["box_name"] = $comic->name();
		$out["box_preview"] = $comic->preview();
		//
		// Global Rate
		$rates = WCReadRate::I()->comicReadRate($comic);
		$out["box_rate_global"] = $rates["rate"];
		$out["box_rate_votes"] = $rates["votes"];
		//
		// Personal Rate
		if($wcProfile->allowedTo(WC_PERM_COMIC_SET_RATE, WC_PERM_COMIC_SET_READ)) {
			$out["box_user_rate"] = true;

			$rates = WCReadRate::I()->userComicReadRate($comic);

			if($rates["rate"]["rate"] !== false) {
				$out["box_rate_personal"] = $rates["rate"]["rate"];
			} else {
				$out["box_rate_personal"] = 0;
			}

			if($rates["read"]["read"]) {
				$out["box_read"] = true;
			}
		}
		//
		// Adding an extra CSS class.
		if($css) {
			$out["box_extra_class"] = $css;
		}

		return $out;
	}
	public static function GetDirectoryPreviewAssigns(WCDirectory &$directory, $css = false) {
		$info = array();
		self::ExpandInfo($info, $directory);

		$out = self::GetBundlePreviewAssigns($directory, $css, $info);

		$out["box_what"] = "directory";
		$out["box_action"] = WC_ACTION_DIRECTORY_SHOW;

		return $out;
	}
	public static function GetComicImagePreviewAssigns(WCComicImage &$image, $css = false, $subURI = false) {
		$out = array();

//		global $wcProfile;
//
//		//
//		// Basic information
//		$out["box_id"] = $image->id();
//		$out["box_image_id"] = "IMG_IM_{$image->id()}";
//		$out["box_name"] = $image->name();
//		$out["box_preview"] = $image->preview();
//		//
//		// Global Rate
//		$rates = WCReadRate::I()->imageReadRate($image);
//		$out["box_rate_global"] = $rates["rate"];
//		$out["box_rate_votes"] = $rates["votes"];
//		//
//		// Personal Rate
//		if($wcProfile->allowedTo(WC_PERM_IMAGE_SET_RATE, WC_PERM_IMAGE_SET_READ)) {
//			$out["box_user_rate"] = true;
//
//			$rates = WCReadRate::I()->userImageReadRate($image);
//
//			if($rates["rate"]["rate"] !== false) {
//				$out["box_rate_personal"] = $rates["rate"]["rate"];
//			} else {
//				$out["box_rate_personal"] = 0;
//			}
//
//			if($rates["read"]["read"]) {
//				$out["box_read"] = true;
//			}
//		}
//		//
//		// Adding an extra CSS class.
//		if($css) {
//			$out["box_extra_class"] = $css;
//		}

		return $out;
	}
	public static function GetImagePreviewAssigns(WCImage &$image, $css = false, $flowUri = false) {
		$out = array();

		global $wcProfile;

		//
		// Basic information
		$out["box_id"] = $image->id();
		$out["box_image_id"] = "IMG_IM_{$image->id()}";
		$out["box_name"] = $image->name();
		$out["box_preview"] = $image->preview();
		//
		// Global Rate
		$rates = WCReadRate::I()->imageReadRate($image);
		$out["box_rate_global"] = $rates["rate"];
		$out["box_rate_votes"] = $rates["votes"];
		//
		// Personal Rate
		if($wcProfile->allowedTo(WC_PERM_IMAGE_SET_RATE, WC_PERM_IMAGE_SET_READ)) {
			$out["box_user_rate"] = true;

			$rates = WCReadRate::I()->userImageReadRate($image);

			if($rates["rate"]["rate"] !== false) {
				$out["box_rate_personal"] = $rates["rate"]["rate"];
			} else {
				$out["box_rate_personal"] = 0;
			}

			if($rates["read"]["read"]) {
				$out["box_read"] = true;
			}
		}
		//
		// Adding an extra CSS class.
		if($css) {
			$out["box_extra_class"] = $css;
		}
		//
		// Adding an extra flow uri.
		if($flowUri) {
			$out["box_flow_uri"] = $flowUri;
		}

		return $out;
	}
	public static function GetRuleActionPreviewAssigns(WCRuleAction &$action, $css = false) {
		$lang = WCTranslate::I();

		$info = array();
		$out["box_id"] = $action->id();
		$out["box_name"] = $action->name();
		$out["box_action"] = "rule:".strtolower($action->type()).":edit";

		$rule = $action->rule();
		foreach(WCViewInfoFactory::LoadInfo($rule) as $key => $value) {
			$out["box_rule_{$key}"] = $value;
		}
		foreach(WCViewInfoFactory::LoadInterfacesInfo($rule) as $key => $value) {
			$out["box_rule_{$key}"] = $value;
		}

		$info[] = array(
			"what" => $lang->translate("label_type"),
			"value" => $action->type()
		);
		if($action->mode()) {
			$info[] = array(
				"what" => $lang->translate("label_mode"),
				"value" => $action->mode()
			);
		}
		$out["box_info"] = $info;

		return $out;
	}
	public static function GetRulePreviewAssigns(WCRule &$rule, $css = false) {
		global $wcProfile;

		$info = array();
		self::ExpandInfo($info, $rule);

		if($rule->conflict) {
			$css = "{$css} WithIssues";
		}

		$out = self::GetBundlePreviewAssigns($rule, $css, $info);


		$out["box_what"] = "rule";
		$out["box_action"] = WC_ACTION_RULE_SHOW;

		if($wcProfile->allowedTo(WC_PERM_RULE_REMOVE)) {
			$out["box_removable"] = "rule";
		}

		return $out;
	}
	public static function GetTagPreviewAssigns(WCTag &$tag, $css = false) {
		global $wcSite;

		$info = array();
		self::ExpandInfo($info, $tag);

		$out = self::GetBundlePreviewAssigns($tag, $css, $info);

		$out["box_what"] = "tag";
		$out["box_action"] = WC_ACTION_TAG_SHOW;

		if($wcSite["wiki"]) {
			$out["box_wiki"] = true;
			$out["box_wiki_url"] = "{$wcSite["wiki"]}WCTag:{$tag->name}";
		}

		return $out;
	}
	//
	// Protected class methods.
	protected static function ExpandInfo(&$info, WCRepresentation &$item) {
		$lang = WCTranslate::I();

		if($item instanceof WCCountComics && $item->hasComics()) {
			$info[] = array(
				"what" => $lang->translate("label_comics"),
				"value" => $item->countComics()
			);
		}
		if($item instanceof WCCountImages && $item->hasImages()) {
			$info[] = array(
				"what" => $lang->translate("label_images"),
				"value" => $item->countImages()
			);
		}
		if($item instanceof WCCountDirectories && $item->hasDirectories()) {
			$info[] = array(
				"what" => $lang->translate("label_directories"),
				"value" => $item->countDirectories()
			);
		}
		if($item instanceof WCCountCollections && $item->hasCollections()) {
			$info[] = array(
				"what" => $lang->translate("label_collections"),
				"value" => $item->countCollections()
			);
		}
		if($item instanceof WCCountActions) {
			$info[] = array(
				"what" => $lang->translate("label_actions"),
				"value" => $item->countActions()
			);
		}
	}
}
